package com.zhou.JavaAndroidFirm.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.bean.CommentItem;
import com.zhou.JavaAndroidFirm.widget.CircleMovementMethod;
import com.zhou.JavaAndroidFirm.widget.CommentListView;
import com.zhou.JavaAndroidFirm.widget.NameClickListener;
import com.zhou.JavaAndroidFirm.widget.NameClickable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/7/25 0025
 * @des 评论适配器
 */
public class CommentAdapter {

    private Context mContext;
    private CommentListView mListview;
    private List<CommentItem> mDatas;

    public CommentAdapter(Context context) {
        mContext = context;
        mDatas = new ArrayList<CommentItem>();
    }

    public CommentAdapter(Context context, List<CommentItem> datas) {
        mContext = context;
        setDatas(datas);
    }

    public void bindListView(CommentListView listView) {
        if (listView == null) {
            throw new IllegalArgumentException("CommentListView is null....");
        }
        mListview = listView;
    }

    public void setDatas(List<CommentItem> datas) {
        if (datas == null) {
            datas = new ArrayList<CommentItem>();
        }

        mDatas = datas;
    }

    public int getCount() {
        if (mDatas == null) {
            return 0;
        }
        return mDatas.size();
    }

    public CommentItem getItem(int position) {
        if (mDatas == null) {
            return null;
        }
        if (position < mDatas.size()) {
            return mDatas.get(position);
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }


    public View getView(final int position) {
        View convertView = View.inflate(mContext,
                R.layout.item_social_comment, null);
        TextView commentTv = (TextView) convertView.findViewById(R.id.commentTv);

        final CircleMovementMethod circleMovementMethod = new CircleMovementMethod(R.color.circle_name_selector_color,
                R.color.circle_name_selector_color);
        final CommentItem bean = mDatas.get(position);
        String name = bean.getUserNickname();
        //      String id = bean.getId();
        String toReplyName = bean.getAppointUserid() != null ? bean.getAppointUserNickname() : "";

        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(setClickableSpan(name, bean.getId(), 0));
        if (!TextUtils.isEmpty(toReplyName)) {
            builder.append(" 回复 ");
            builder.append(setClickableSpan(toReplyName, bean.getAppointUserNickname(), 1));
        }
        builder.append(": ");
        //转换表情字符
        String content = bean.getContent();
        builder.append(content);
        commentTv.setText(builder);


        commentTv.setMovementMethod(circleMovementMethod);
        commentTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (circleMovementMethod.isPassToTv()) {
                    mListview.getOnItemClickListener().onItemClick(position);
                }
            }
        });
        commentTv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (circleMovementMethod.isPassToTv()) {
                    mListview.getOnItemLongClickListener().onItemLongClick(position);
                }
                return false;
            }
        });
        return convertView;
    }

    public void notifyDataSetChanged() {
        if (mListview == null) {
            throw new NullPointerException("listview is null, please bindListView first...");
        }
        mListview.removeAllViews();
        if (mDatas == null || mDatas.size() == 0) {
            return;
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < mDatas.size(); i++) {
            View view = getView(i);
            if (view == null) {
                throw new NullPointerException("listview item layout is null, please check getView()...");
            }
            mListview.addView(view, layoutParams);

        }
    }

    @NonNull
    private SpannableString setClickableSpan(String name, String id, int pos) {
        SpannableString text = new SpannableString(name);
        text.setSpan(new NameClickable(new NameClickListener(text, id), pos), 0, text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        return text;
    }
}
